extends Control

@onready var texture_rect_2: TextureRect = $TextureRect2

@onready var texture_size_number_1: TextureRect = $TextureRect4
@onready var texture_size_number_2: TextureRect = $TextureRect5

@onready var tip_label: Label = $Label

@onready var audio_player: AudioStreamPlayer = $AudioStreamPlayer

@onready var texture_bonus_win_1: TextureRect = $Control4/TextureRect
@onready var texture_bonus_win_2: TextureRect = $Control4/TextureRect2
@onready var texture_bonus_win_3: TextureRect = $Control4/TextureRect3
@onready var texture_bonus_win_4: TextureRect = $Control4/TextureRect4
@onready var texture_bonus_win_5: TextureRect = $Control4/TextureRect5
@onready var texture_bonus_win_6: TextureRect = $Control4/TextureRect6
@onready var texture_bonus_win_7: TextureRect = $Control4/TextureRect7
@onready var texture_bonus_win_8: TextureRect = $Control4/TextureRect8

@onready var texture_credit_1: TextureRect = $Control4/TextureRect9
@onready var texture_credit_2: TextureRect = $Control4/TextureRect10
@onready var texture_credit_3: TextureRect = $Control4/TextureRect11
@onready var texture_credit_4: TextureRect = $Control4/TextureRect12
@onready var texture_credit_5: TextureRect = $Control4/TextureRect13
@onready var texture_credit_6: TextureRect = $Control4/TextureRect14
@onready var texture_credit_7: TextureRect = $Control4/TextureRect15
@onready var texture_credit_8: TextureRect = $Control4/TextureRect16

@onready var texture_run_1: TextureRect = $Control/TextureRect
@onready var texture_run_2: TextureRect = $Control/TextureRect2
@onready var texture_run_3: TextureRect = $Control/TextureRect3
@onready var texture_run_4: TextureRect = $Control/TextureRect4
@onready var texture_run_5: TextureRect = $Control/TextureRect5
@onready var texture_run_6: TextureRect = $Control/TextureRect6
@onready var texture_run_7: TextureRect = $Control/TextureRect7
@onready var texture_run_8: TextureRect = $Control/TextureRect8
@onready var texture_run_9: TextureRect = $Control/TextureRect9
@onready var texture_run_10: TextureRect = $Control/TextureRect10
@onready var texture_run_11: TextureRect = $Control/TextureRect11
@onready var texture_run_12: TextureRect = $Control/TextureRect12
@onready var texture_run_13: TextureRect = $Control/TextureRect13
@onready var texture_run_14: TextureRect = $Control/TextureRect14
@onready var texture_run_15: TextureRect = $Control/TextureRect15
@onready var texture_run_16: TextureRect = $Control/TextureRect16
@onready var texture_run_17: TextureRect = $Control/TextureRect17
@onready var texture_run_18: TextureRect = $Control/TextureRect18
@onready var texture_run_19: TextureRect = $Control/TextureRect19
@onready var texture_run_20: TextureRect = $Control/TextureRect20
@onready var texture_run_21: TextureRect = $Control/TextureRect21
@onready var texture_run_22: TextureRect = $Control/TextureRect22
@onready var texture_run_23: TextureRect = $Control/TextureRect23
@onready var texture_run_24: TextureRect = $Control/TextureRect24

@onready var texture_console_1: TextureRect = $Control2/TextureRect
@onready var texture_console_2: TextureRect = $Control2/TextureRect2
@onready var texture_console_3: TextureRect = $Control2/TextureRect3
@onready var texture_console_4: TextureRect = $Control2/TextureRect4
@onready var texture_console_5: TextureRect = $Control2/TextureRect5
@onready var texture_console_6: TextureRect = $Control2/TextureRect6

@onready var texture_number_1: TextureRect = $Control3/TextureRect
@onready var texture_number_2: TextureRect = $Control3/TextureRect2
@onready var texture_number_3: TextureRect = $Control3/TextureRect3
@onready var texture_number_4: TextureRect = $Control3/TextureRect4
@onready var texture_number_5: TextureRect = $Control3/TextureRect5
@onready var texture_number_6: TextureRect = $Control3/TextureRect6
@onready var texture_number_7: TextureRect = $Control3/TextureRect7
@onready var texture_number_8: TextureRect = $Control3/TextureRect8
@onready var texture_number_9: TextureRect = $Control3/TextureRect9
@onready var texture_number_10: TextureRect = $Control3/TextureRect10
@onready var texture_number_11: TextureRect = $Control3/TextureRect11
@onready var texture_number_12: TextureRect = $Control3/TextureRect12
@onready var texture_number_13: TextureRect = $Control3/TextureRect13
@onready var texture_number_14: TextureRect = $Control3/TextureRect14
@onready var texture_number_15: TextureRect = $Control3/TextureRect15
@onready var texture_number_16: TextureRect = $Control3/TextureRect16

@onready var button_console_1: TextureButton = $HBoxContainer2/TextureButton
@onready var button_console_2: TextureButton = $HBoxContainer2/TextureButton2
@onready var button_console_3: TextureButton = $HBoxContainer2/TextureButton3
@onready var button_console_4: TextureButton = $HBoxContainer2/TextureButton4
@onready var button_console_5: TextureButton = $HBoxContainer2/TextureButton5
@onready var button_console_6: TextureButton = $HBoxContainer2/TextureButton6

@onready var texture_button_1: TextureButton = $HBoxContainer3/TextureButton
@onready var texture_button_2: TextureButton = $HBoxContainer3/TextureButton2
@onready var texture_button_3: TextureButton = $HBoxContainer3/TextureButton3
@onready var texture_button_4: TextureButton = $HBoxContainer3/TextureButton4
@onready var texture_button_5: TextureButton = $HBoxContainer3/TextureButton5
@onready var texture_button_6: TextureButton = $HBoxContainer3/TextureButton6
@onready var texture_button_7: TextureButton = $HBoxContainer3/TextureButton7
@onready var texture_button_8: TextureButton = $HBoxContainer3/TextureButton8

var credit: int = 12345678
var bonus_win: int = 0
var size_number: int = 0
var temp_bonus_win: int = 0
var last_position: int = 0
var transfer_initial_bonus_win: int = 0
var transfer_initial_credit: int = 0
var current_index: int = 0

var is_click = true

var bet_array: Array[int] = [0, 0, 0, 0, 0, 0, 0, 0]
var last_bet_array: Array[int] = [0, 0, 0, 0, 0, 0, 0, 0]
var run_odds_array: Array[int] = [-1, -1, 50, 100, 5, 3, -1, -2, 3, 0, 5, 3, -1, -1, 3, -2, 5, 3, -1, -2, 3, 0, 5, 3]
var current_duration_list: Array[int] = []

var button_name_array: Array[String] = ["bar", "77", "双星", "西瓜", "铃铛", "芒果", "桔子", "苹果"]

var number_array: Array[Texture2D] = [
	preload("res://image/top_0.png"),
	preload("res://image/top_1.png"),
	preload("res://image/top_2.png"),
	preload("res://image/top_3.png"),
	preload("res://image/top_4.png"),
	preload("res://image/top_5.png"),
	preload("res://image/top_6.png"),
	preload("res://image/top_7.png"),
	preload("res://image/top_8.png"),
	preload("res://image/top_9.png")
]

var credit_array: Array[TextureRect] = []
var bonus_win_array: Array[TextureRect] = []
var run_array: Array[TextureRect] = []
var bet_number_array: Array[TextureRect] = []
var magnification_big_array: Array[TextureRect] = []
var magnification_small_array: Array[TextureRect] = []

var win_music_array: Array[AudioStream] = [
	preload("res://audio/C01.ogg"),
	preload("res://audio/C01.ogg"),
	preload("res://audio/C01.ogg"),
	preload("res://audio/C01.ogg"),
	preload("res://audio/C01.ogg")
]

var sound_effects: Array[AudioStream] = [
	preload("res://audio/hit.ogg"),
	preload("res://audio/ding.ogg"),
	preload("res://audio/rotate_start.ogg"),
	preload("res://audio/rotating.ogg"),
	preload("res://audio/rotate_end.ogg")
]

var win_sound_array: Array[String] = [
	"res://audio/C01.ogg",
	"res://audio/C02.ogg",
	"res://audio/C03.ogg",
	"res://audio/C04.ogg",
	"res://audio/C05.ogg",
	"res://audio/C06.ogg",
	"res://audio/C07.ogg",
	"res://audio/C08.ogg",
	"res://audio/C09.ogg",
	"res://audio/C10.ogg",
	"res://audio/C11.ogg",
	"res://audio/C12.ogg",
	"res://audio/C13.ogg",
	"res://audio/C14.ogg",
	"res://audio/C15.ogg"
]
var current_playing_list: Array[AudioStream] = []

const EMPTY_BET_ARRAY: Array[int] = [0, 0, 0, 0, 0, 0, 0, 0]
const ROTATE_SOUND_SEQUENCE: Array[AudioStream] = [
	preload("res://audio/rotate_start.ogg"),
	preload("res://audio/rotating.ogg"),
	preload("res://audio/rotate_end.ogg"),
	preload("res://audio/ding.ogg")
]
const DING_SOUND: AudioStream = preload("res://audio/ding.ogg")
const LUCKY_SUCCESS_SOUND: AudioStream = preload("res://audio/luklySuccess.ogg")
const LUCKY_FAIL_SOUND: AudioStream = preload("res://audio/luklyFail.ogg")

const SOUND_MAP: Dictionary = {
	"苹果": "res://audio/Y101.ogg",
	"桔子": "res://audio/Y102.ogg",
	"芒果": "res://audio/Y103.ogg",
	"铃铛": "res://audio/Y104.ogg",
	"西瓜": "res://audio/Y105.ogg",
	"双星": "res://audio/Y106.ogg",
	"77": "res://audio/Y107.ogg",
	"bar": "res://audio/Y108.ogg",
	"大三元": "res://audio/Y114.ogg",
	"小三元": "res://audio/Y115.ogg",
	"大四喜": "res://audio/Y117.ogg",
	"纵横四海": "res://audio/Y118.ogg",
	"大满贯": "res://audio/Y120.ogg",
	"天女散花": "res://audio/Y122.ogg",
	"开火车": "res://audio/Y124.ogg",
	"送灯": "res://audio/Y131.ogg",
	"真可惜啊": "res://audio/zhenkexi.ogg",
	"中了": "res://audio/zhongle.ogg",
}
const POSITION_TO_BET_INDEX: Dictionary = {0: 6, 1: 4, 2: 0, 3: 0, 4: 7, 5: 7, 6: 5, 7: 3, 8: 3, 10: 7, 11: 6, 12: 6, 13: 4, 14: 1, 15: 1, 16: 7, 17: 5, 18: 5, 19: 2, 20: 2, 22: 7, 23: 4}

var audio_playlist: Array[AudioItem] = []

class AudioItem:
	var tag: int = 0
	# 音频文件路径
	var audio_path: String = ""
	# 该音频的播放间隔时间（秒）
	var custom_interval: float = 0.1
	
	func _init(audio_tag: int = 0, path: String = "", interval: float = 0.1) -> void:
		"""初始化音频项"""
		tag = audio_tag
		audio_path = path
		custom_interval = interval
	
	func get_info() -> String:
		"""获取音频项信息"""
		return "音频: %s, 间隔: %.2fs" % [audio_path, custom_interval]

func get_audio_item(audio_tag: int, path: String, interval: float) -> AudioItem:
	var item1 = AudioItem.new()
	item1.tag = audio_tag
	item1.audio_path = path
	item1.custom_interval = interval
	return item1


func set_custom_playlist(audio_items: Array[AudioItem]) -> void:
	"""设置自定义播放列表"""
	if audio_player.playing:
		audio_player.stop()
	audio_playlist = audio_items
	current_index = 0
	await start_playlist()

func start_playlist() -> void:
	"""开始播放播放列表"""
	current_index = 0
	await play_next()
func play_next() -> void:
	"""播放下一个音频"""
	if current_index >= audio_playlist.size():
		return
	var current_audio_item: AudioItem = audio_playlist[current_index]

	var audio_stream = load(current_audio_item.audio_path)
	if audio_stream:
		_execute_audio_start_callback(current_audio_item, current_index)
		audio_player.stream = audio_stream
		audio_player.play()
		await audio_player.finished
		_execute_audio_finished_callback(current_audio_item, current_index)
		if current_audio_item.custom_interval > 0:
			await get_tree().create_timer(current_audio_item.custom_interval).timeout
		current_index += 1
		await play_next()
	else:
		current_index += 1
		await play_next()

func _execute_audio_start_callback(audio_item: AudioItem, index: int) -> void:
	if on_audio_start_callback.is_valid():
		on_audio_start_callback.call(audio_item, index)

var on_audio_start_callback: Callable = Callable()

func _execute_audio_finished_callback(audio_item: AudioItem, index: int) -> void:
	if on_audio_finished_callback.is_valid():
		on_audio_finished_callback.call(audio_item, index)

var on_audio_finished_callback: Callable = Callable()

# 设置音频开始播放时的回调函数
func set_on_audio_start_callback(callback: Callable) -> void:
	"""设置音频开始播放时的回调函数"""
	on_audio_start_callback = callback

# 设置音频播放完成时的回调函数
func set_on_audio_finished_callback(callback: Callable) -> void:
	"""设置音频播放完成时的回调函数"""
	on_audio_finished_callback = callback

var timestampaaaa: int = 0

func on_audio_start_handler(audio_item: AudioItem, index: int) -> void:
	# timestampaaaa = Time.get_unix_time_from_system()
	if timestampaaaa == 0:
		timestampaaaa = Time.get_unix_time_from_system()
	"""音频开始播放时的回调处理函数"""
	# print("🎵 开始播放第%d个音频: %s" % [index + 1, audio_item.audio_path])

# 音频播放完成时的处理函数
func on_audio_finished_handler(audio_item: AudioItem, index: int) -> void:
	"""音频播放完成时的回调处理函数"""
	# 4.7
	# 5.4
	# print("✅ 第%d个音频播放完成: %s" % [index + 1, audio_item.audio_path])
	print(Time.get_unix_time_from_system()-timestampaaaa)
	

func show_tip(text: String, duration: float = 2.0):
	tip_label.text = text
	tip_label.visible = true
	await get_tree().create_timer(duration).timeout
	tip_label.visible = false

func int_to_digit_array(n: int) -> Array[int]:
	var abs_n = abs(n)
	var digits: Array[int] = []
	for c in str(abs_n):
		digits.append(int(c))
	return digits


func _ready() -> void:
	credit_array = [
		texture_credit_1,
		texture_credit_2,
		texture_credit_3,
		texture_credit_4,
		texture_credit_5,
		texture_credit_6,
		texture_credit_7,
		texture_credit_8,
	]
	bonus_win_array = [
		texture_bonus_win_1,
		texture_bonus_win_2,
		texture_bonus_win_3,
		texture_bonus_win_4,
		texture_bonus_win_5,
		texture_bonus_win_6,
		texture_bonus_win_7,
		texture_bonus_win_8,
	]
	run_array = [
		texture_run_1,
		texture_run_2,
		texture_run_3,
		texture_run_4,
		texture_run_5,
		texture_run_6,
		texture_run_7,
		texture_run_8,
		texture_run_9,
		texture_run_10,
		texture_run_11,
		texture_run_12,
		texture_run_13,
		texture_run_14,
		texture_run_15,
		texture_run_16,
		texture_run_17,
		texture_run_18,
		texture_run_19,
		texture_run_20,
		texture_run_21,
		texture_run_22,
		texture_run_23,
		texture_run_24,
	]
	bet_number_array = [
		texture_number_1,
		texture_number_2,
		texture_number_3,
		texture_number_4,
		texture_number_5,
		texture_number_6,
		texture_number_7,
		texture_number_8,
		texture_number_9,
		texture_number_10,
		texture_number_11,
		texture_number_12,
		texture_number_13,
		texture_number_14,
		texture_number_15,
		texture_number_16,
	]
	magnification_big_array = [
		texture_console_3,
		texture_console_2,
		texture_console_1,
	]
	magnification_small_array = [
		texture_console_6,
		texture_console_5,
		texture_console_4,
	]
	for element in magnification_big_array:
		element.visible = false
	for element in magnification_small_array:
		element.visible = false
	for element in run_array:
		element.visible = false
	set_top_credit()
	set_top_bonus_win()
	set_bottom_number()
	set_size_number()
	set_on_audio_start_callback(Callable(self, "on_audio_start_handler"))
	set_on_audio_finished_callback(Callable(self, "on_audio_finished_handler"))

func _on_texture_button_pressed() -> void:
	_on_bottom_button_pressed(0)

func _on_texture_button_2_pressed() -> void:
	_on_bottom_button_pressed(1)

func _on_texture_button_3_pressed() -> void:
	_on_bottom_button_pressed(2)

func _on_texture_button_4_pressed() -> void:
	_on_bottom_button_pressed(3)

func _on_texture_button_5_pressed() -> void:
	_on_bottom_button_pressed(4)

func _on_texture_button_6_pressed() -> void:
	_on_bottom_button_pressed(5)

func _on_texture_button_7_pressed() -> void:
	_on_bottom_button_pressed(6)

func _on_texture_button_8_pressed() -> void:
	_on_bottom_button_pressed(7)

func _on_texture_button_11_pressed() -> void:
	print("_on_texture_button_11_pressed")
	if not _check_can_operate():
		return
	if bonus_win > 0:
		return
	if credit < 8:
		return
	set_custom_playlist([get_audio_item(4, "res://audio/ding.ogg", 0)])
	for i in bet_array.size():
		if bet_array[i] < 99:
			bet_array[i] += 1
	set_bottom_number()

func _on_texture_button_12_pressed() -> void:
	print("_on_texture_button_12_pressed")
	if not _check_can_operate():
		return
	if bonus_win <= 0:
		return
	if bonus_win < temp_bonus_win * 2:
		set_custom_playlist([get_audio_item(4, "res://audio/ding.ogg", 0)])
		var target_bonus: int = temp_bonus_win * 2
		var total_assets: int = bonus_win + credit
		if target_bonus < total_assets:
			bonus_win = target_bonus
			credit = total_assets - target_bonus
		else:
			bonus_win = total_assets
			credit = 0
	set_top_credit()
	set_top_bonus_win()

func _on_texture_button_13_pressed() -> void:
	print("_on_texture_button_13_pressed")
	if not _check_can_operate():
		return
	if bonus_win <= 0:
		return
	set_custom_playlist([get_audio_item(4, "res://audio/ding.ogg", 0)])
	if bonus_win > 0:
		bonus_win -= 1
		credit += 1
		set_top_credit()
		set_top_bonus_win()
		if bonus_win == 0:
			last_bet_array = bet_array.duplicate()
			bet_array = EMPTY_BET_ARRAY.duplicate()
			set_bottom_number()

func _on_texture_button_14_pressed() -> void:
	print("_on_texture_button_14_pressed")
	_play_size_game(true)

func _on_texture_button_15_pressed() -> void:
	print("_on_texture_button_15_pressed")
	_play_size_game(false)


func _on_texture_button_16_pressed() -> void:
	print("GO按钮的点击事件")
	if credit <= 0 and bonus_win <= 0:
		show_tip("请先充值!", 2.0)
		return
	var bet_sum: int = _get_bet_sum(bet_array)
	if bet_sum == 0:
		var last_bet_sum: int = _get_bet_sum(last_bet_array)
		if last_bet_sum == 0:
			show_tip("请先押注!", 2.0)
			return
		else:
			bet_array = last_bet_array.duplicate()
			set_bottom_number()
			credit -= last_bet_sum
			set_top_credit()
	if not is_click:
		return
	if bonus_win > 0:
		is_click = false
		transfer_initial_bonus_win = bonus_win
		transfer_initial_credit = credit
		set_custom_playlist([get_audio_item(2, "res://audio/hitLukly.ogg", 0)])
		last_bet_array = bet_array.duplicate()
		bet_array = EMPTY_BET_ARRAY.duplicate()
		set_bottom_number()
		var transfer_tween: Tween = create_tween()
		transfer_tween.set_trans(Tween.TRANS_LINEAR).set_ease(Tween.EASE_IN_OUT)
		transfer_tween.tween_method(Callable(self, "_set_move"), 0.0, float(transfer_initial_bonus_win), 0.836)
		await transfer_tween.finished
		bonus_win = 0
		credit = transfer_initial_credit + transfer_initial_bonus_win
		set_top_credit()
		set_top_bonus_win()
		button_console_1.disabled = false
		is_click = true
		return
	is_click = false
	var random_number: int = randi_range(72, 95)
	var _final_index_position: int = (random_number + last_position) % 24
	var win_price: int = 0
	if _final_index_position == 9 or _final_index_position == 21:
		print('11111111111')
		set_custom_playlist([
			get_audio_item(3, "res://audio/rotate_start.ogg", 0),
			get_audio_item(3, "res://audio/rotating.ogg", 0),
			get_audio_item(3, "res://audio/rotate_end.ogg", 0),
			get_audio_item(3, "res://audio/ding.ogg", 0),
			get_audio_item(3, "res://audio/luklyFail.ogg", 0), ])
	else:
		print('22222222222')
		var bet_index: int = get_final_number(_final_index_position)
		var bet_amount: int = bet_array[bet_index]
		if bet_amount == 0:
			set_custom_playlist([
			get_audio_item(3, "res://audio/rotate_start.ogg", 0),
			get_audio_item(3, "res://audio/rotating.ogg", 0),
			get_audio_item(3, "res://audio/rotate_end.ogg", 0),
			get_audio_item(3, "res://audio/ding.ogg", 0), ])
		else:
			print("win_price: ", win_price)
			var current_odds: int = run_odds_array[_final_index_position]
			var random_num: int = randi() % 3 + 1
			if current_odds == -1:
				win_price = (random_num * 5 + 5) * bet_amount
			elif current_odds == -2:
				win_price = ((random_num * 5 + 5) * 2) * bet_amount
			else:
				win_price = bet_amount * current_odds
			var win_sound_sequence: Array[AudioStream] = ROTATE_SOUND_SEQUENCE.duplicate()
			set_custom_playlist([
			get_audio_item(3, "res://audio/rotate_start.ogg", 0),
			get_audio_item(3, "res://audio/rotating.ogg", 0),
			get_audio_item(3, "res://audio/rotate_end.ogg", 0),
			get_audio_item(3, "res://audio/ding.ogg", 0),
			get_audio_item(3, get_sound_for_name(button_name_array[bet_index]), 0),
			get_audio_item(3, win_sound_array[randi() % win_sound_array.size()], 0), ])
	var spin_tween: Tween = tween_three_phase(Callable(self, "_set_alpha"),float(last_position),float(last_position + random_number),5-1.5)
	await spin_tween.finished
	last_bet_array = bet_array.duplicate()
	if win_price <= 0:
		bet_array = EMPTY_BET_ARRAY.duplicate()
		set_bottom_number()
		_update_button_states_for_bet()
	else:
		_update_button_states_for_win()
	last_position = _final_index_position
	bonus_win = win_price
	temp_bonus_win = win_price
	set_top_bonus_win()
	is_click = true

func _set_alpha(value):
	var new_index: int = int(value) % 24
	for j in run_array.size():
		run_array[j].visible = (j == new_index)
	var temp_index: int = int(value) % 3
	for j in magnification_big_array.size():
		magnification_big_array[j].visible = (j == temp_index)
	for j in magnification_small_array.size():
		magnification_small_array[j].visible = (j == temp_index)

func _update_button_states_for_win() -> void:
	button_console_1.disabled = true
	button_console_2.disabled = false
	button_console_3.disabled = false
	button_console_4.disabled = false
	button_console_5.disabled = false
	button_console_6.disabled = false

func tween_three_phase(
	method: Callable,
	from: float,
	to: float,
	duration: float,
	accel_ratio: float = 0.2,
	const_ratio: float = 0.5,
	decel_ratio: float = 0.3
) -> Tween:
	var tween: Tween = create_tween()
	var total_range: float = to - from
	var accel_duration: float = duration * accel_ratio
	var const_duration: float = duration * const_ratio
	var decel_duration: float = duration * decel_ratio
	var accel_value_ratio: float = 0.15
	var const_value_ratio: float = 0.70
	var accel_end: float = from + total_range * accel_value_ratio
	var const_start: float = accel_end
	var const_end: float = from + total_range * (accel_value_ratio + const_value_ratio)
	var decel_start: float = const_end

	(
		tween
		.tween_method(method, from, accel_end, accel_duration)
		.set_trans(Tween.TRANS_SINE)
		.set_ease(Tween.EASE_IN)
	)
	(
		tween
		.tween_method(method, const_start, const_end, const_duration)
		.set_trans(Tween.TRANS_LINEAR)
		.set_ease(Tween.EASE_IN_OUT)
	)
	(
		tween
		.tween_method(method, decel_start, to, decel_duration)
		.set_trans(Tween.TRANS_SINE)
		.set_ease(Tween.EASE_OUT)
	)
	return tween


func get_sound_for_name(sound_name: String) -> String:
	return SOUND_MAP.get(sound_name, LUCKY_FAIL_SOUND)

func get_final_number(index: int) -> int:
	return POSITION_TO_BET_INDEX.get(index, -1)

func _set_move(value: float):
	var transferred = int(value)
	bonus_win = transfer_initial_bonus_win - transferred
	credit = transfer_initial_credit + transferred
	set_top_credit()
	set_top_bonus_win()

func _get_bet_sum(bet_arr: Array[int]) -> int:
	return bet_arr.reduce(func(accum, num): return accum + num, 0)

func _on_bottom_button_pressed(index: int) -> void:
	if not _check_can_operate():
		return
	if bonus_win > 0:
		return
	var last_bet: int = bet_array[index]
	if last_bet >= 99:
		return
	if credit >= 1:
		set_custom_playlist([get_audio_item(1, "res://audio/ding.ogg", 0)])
		bet_array[index] = last_bet + 1
		credit -= 1
		set_top_credit()
		_update_button_states_for_bet()
		_update_single_bet_display(index)
	

func _check_can_operate() -> bool:
	if credit <= 0 and bonus_win <= 0:
		show_tip("请先充值!", 2.0)
		return false
	if not is_click:
		return false
	return true

func set_top_credit() -> void:
	_update_number_display(credit, credit_array)

func _update_number_display(value: int, display_array: Array[TextureRect]) -> void:
	for element in display_array:
		element.modulate.a = 0
	var digit_array: Array[int] = int_to_digit_array(value)
	var padding: int = 8 - digit_array.size()
	for i in range(digit_array.size()):
		var digit: int = digit_array[i]
		var texture_rect: TextureRect = display_array[i + padding]
		texture_rect.modulate.a = 1
		texture_rect.texture = number_array[digit]

func _update_button_states_for_bet() -> void:
	button_console_1.disabled = false
	button_console_2.disabled = true
	button_console_3.disabled = true
	button_console_4.disabled = true
	button_console_5.disabled = true
	button_console_6.disabled = false

func _update_single_bet_display(index: int) -> void:
	var value: int = bet_array[index]
	var tens_index: int = index * 2
	var ones_index: int = index * 2 + 1

	if value < 10:
		bet_number_array[tens_index].visible = false
		bet_number_array[ones_index].texture = number_array[value]
	else:
		var digits: Array[int] = int_to_digit_array(value)
		bet_number_array[tens_index].visible = true
		bet_number_array[tens_index].texture = number_array[digits.front()]
		bet_number_array[ones_index].texture = number_array[digits.back()]

func set_top_bonus_win() -> void:
	_update_number_display(bonus_win, bonus_win_array)

func set_bottom_number() -> void:
	for index in bet_array.size():
		var value: int = bet_array[index]
		var tens_index: int = index * 2
		var ones_index: int = index * 2 + 1

		if value < 10:
			bet_number_array[tens_index].visible = false
			bet_number_array[ones_index].texture = number_array[value]
		else:
			var digits: Array[int] = int_to_digit_array(value)
			bet_number_array[tens_index].visible = true
			bet_number_array[tens_index].texture = number_array[digits.front()]
			bet_number_array[ones_index].texture = number_array[digits.back()]

func set_size_number() -> void:
	if size_number < 10:
		texture_size_number_1.visible = false
		texture_size_number_2.texture = number_array[size_number]
	else:
		var digit_array: Array[int] = int_to_digit_array(size_number)
		texture_size_number_1.visible = true
		texture_size_number_1.texture = number_array[digit_array.front()]
		texture_size_number_2.texture = number_array[digit_array.back()]

func _play_size_game(is_small: bool) -> void:
	if not _check_can_operate():
		return
	if bonus_win <= 0:
		return
	is_click = false
	var random_number: int = randi_range(13, 25)
	var durations: Array[float] = []
	for i in range(random_number, -1, -1):
		if i == 0:
			durations.append(0.5)
		elif i == 1:
			durations.append(0.4)
		elif i == 2:
			durations.append(0.3)
		elif i == 3:
			durations.append(0.2)
		elif i == 4:
			durations.append(0.1)
		else:
			durations.append(0)
	var sound_list : Array[AudioItem] = []
	for i in range(random_number):
		sound_list.append(get_audio_item(5, "res://audio/hit.ogg", 0))
	set_custom_playlist(sound_list)
	var won: bool = (size_number < 7) if is_small else (size_number > 7)
	if size_number == 7:
		print("平: ")
	elif won:
		set_custom_playlist([get_audio_item(1, "res://audio/luklySuccess.ogg", 0),get_audio_item(1, win_sound_array[randi() % win_sound_array.size()], 0)])
		bonus_win *= 2
		temp_bonus_win = bonus_win
		set_top_bonus_win()
	else:
		set_custom_playlist([get_audio_item(1, "res://audio/luklyFail.ogg", 0)])
		bonus_win = 0
		temp_bonus_win = 0
		set_top_bonus_win()
		bet_array = EMPTY_BET_ARRAY.duplicate()
		set_bottom_number()
	is_click = true